added Feb 2001 SDK
[windows-sources.git] / shared source / sscli20 / jscript / engine / enumwrapper.cs
blob5936c6546ca23bff257fb5dd919175ee002d54d4
1 // ==++==
2 //
3 //
4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
5 //
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
10 //
11 // You must not remove this notice, or any other, from this software.
12 //
13 //
14 // ==--==
16 namespace Microsoft.JScript {
18 using System;
19 using System.Reflection;
20 using System.Reflection.Emit;
22 internal abstract class EnumWrapper : IConvertible{
23 internal abstract Object value {
24 get;
27 internal abstract Type type {
28 get;
31 internal abstract String name {
32 get;
35 internal virtual IReflect classScopeOrType {
36 get {
37 return this.type;
41 TypeCode IConvertible.GetTypeCode(){
42 return Convert.GetTypeCode(this.value);
45 bool IConvertible.ToBoolean(IFormatProvider provider){
46 return ((IConvertible)this.value).ToBoolean(provider);
49 char IConvertible.ToChar(IFormatProvider provider){
50 return ((IConvertible)this.value).ToChar(provider);
53 sbyte IConvertible.ToSByte(IFormatProvider provider){
54 return ((IConvertible)this.value).ToSByte(provider);
57 byte IConvertible.ToByte(IFormatProvider provider){
58 return ((IConvertible)this.value).ToByte(provider);
61 short IConvertible.ToInt16(IFormatProvider provider){
62 return ((IConvertible)this.value).ToInt16(provider);
65 ushort IConvertible.ToUInt16(IFormatProvider provider){
66 return ((IConvertible)this.value).ToUInt16(provider);
69 int IConvertible.ToInt32(IFormatProvider provider){
70 return ((IConvertible)this.value).ToInt32(provider);
73 uint IConvertible.ToUInt32(IFormatProvider provider){
74 return ((IConvertible)this.value).ToUInt32(provider);
77 long IConvertible.ToInt64(IFormatProvider provider){
78 return ((IConvertible)this.value).ToInt64(provider);
81 ulong IConvertible.ToUInt64(IFormatProvider provider){
82 return ((IConvertible)this.value).ToUInt64(provider);
85 float IConvertible.ToSingle(IFormatProvider provider){
86 return ((IConvertible)this.value).ToSingle(provider);
89 double IConvertible.ToDouble(IFormatProvider provider){
90 return ((IConvertible)this.value).ToDouble(provider);
93 Decimal IConvertible.ToDecimal(IFormatProvider provider){
94 return ((IConvertible)this.value).ToDecimal(provider);
97 DateTime IConvertible.ToDateTime(IFormatProvider provider){
98 return ((IConvertible)this.value).ToDateTime(provider);
101 String IConvertible.ToString(IFormatProvider provider){
102 return ((IConvertible)this.value).ToString(provider);
105 Object IConvertible.ToType(Type conversionType, IFormatProvider provider){
106 return ((IConvertible)this.value).ToType(conversionType, provider);
109 internal Object ToNumericValue(){
110 return this.value;
113 public override String ToString(){
114 if (this.name != null) return this.name;
115 FieldInfo[] fields = this.type.GetFields(BindingFlags.Static|BindingFlags.Public);
116 foreach (FieldInfo field in fields)
117 if (StrictEquality.JScriptStrictEquals(this.value, field.GetValue(null))) return field.Name;
118 return Convert.ToString(this.value);
123 internal sealed class DeclaredEnumValue : EnumWrapper {
124 private String _name;
125 internal ClassScope _classScope;
126 internal Object _value;
128 internal DeclaredEnumValue(Object value, String name, ClassScope classScope){
129 this._name = name;
130 this._classScope = classScope;
131 this._value = value;
134 internal override Object value {
135 get {
136 return this._value;
140 internal override Type type {
141 get {
142 return this._classScope.GetTypeBuilderOrEnumBuilder();
146 internal override String name {
147 get {
148 return this._name;
152 internal override IReflect classScopeOrType {
153 get {
154 return this._classScope;
158 internal void CoerceToBaseType(Type bt, Context errCtx){
159 Object val = 0;
160 AST pval = ((AST)this.value).PartiallyEvaluate();
161 if (pval is ConstantWrapper)
162 val = ((ConstantWrapper)pval).Evaluate();
163 else
164 pval.context.HandleError(JSError.NotConst);
165 try{
166 this._value = Convert.CoerceT(val, bt);
167 }catch{
168 errCtx.HandleError(JSError.TypeMismatch);
169 this._value = Convert.CoerceT(0, bt);
175 internal sealed class MetadataEnumValue : EnumWrapper {
176 private Type _type;
177 private Object _value;
179 internal static Object GetEnumValue(Type type, Object value) {
180 Debug.Assert(type.IsEnum);
181 if (!type.Assembly.ReflectionOnly)
182 return Enum.ToObject(type, value);
183 return new MetadataEnumValue(type, value);
186 private MetadataEnumValue(Type type, Object value) {
187 this._type = type;
188 this._value = value;
191 internal override Object value {
192 get {
193 return this._value;
197 internal override Type type {
198 get {
199 return this._type;
203 internal override String name {
204 get {
205 String name = Enum.GetName(this._type, this._value);
206 if (name == null)
207 name = this._value.ToString();
208 return name;